package com.sjzd.server.controller;


import com.sjzd.server.pojo.Admin;
import com.sjzd.server.pojo.Menu;
import com.sjzd.server.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjzd
 * @since 2021-02-21
 */
@Api("前端页面控制器")
@RestController
@RequestMapping("/system/cfg")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @ApiOperation("根据用户id查询展示页面列表")
    @GetMapping("/menu")
    public List<Menu> getMenusByAdminId() {
        Admin admin =
                (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Menu> menus = (List<Menu>)redisTemplate.opsForValue().get("menu_" + admin.getId());
        if (null == menus) {
            menus = menuService.getMenusByAdminId(admin.getId());
            redisTemplate.opsForValue().set("menu_" + admin.getId(), menus);
        }
        return menus;
    }
}
